import numpy as np
from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.lda import LDA
import matplotlib.pyplot as plt

sklearnIris = datasets.load_iris()
label = sklearnIris.target
irisData = np.matrix(sklearnIris.data)

print "Data matrix shape:", irisData.shape

#standardization of the dataset
colMean = irisData.mean(axis=0)
colStd = irisData.std(axis=0)

print "Column Mean:", colMean
print "Column Std:", colStd

normalizeIrisData = (irisData-colMean)/colStd
print "verify mean:", np.mean(normalizeIrisData, axis=0)

#######################################################################
#PCA - covariance
covarianceMatrix = normalizeIrisData.transpose()*normalizeIrisData
print "covarianceMatrix shape: ", covarianceMatrix.shape
#eigen decomposition
[principle_value, principle_vector] = np.linalg.eig(covarianceMatrix)
print "Principle values: ", principle_value
print "principle_vector shape: ", principle_vector.shape
principle_vector = principle_vector.transpose()
firstTwoCompoment = principle_vector[:,].take([0,1], axis=1)
print "first two compoment shape: ", firstTwoCompoment.shape
#PCA-projection
PCAembedding = irisData*firstTwoCompoment;
print "Emebedding shape: ", PCAembedding.shape
#plt.figure()
#plt.scatter(PCAembedding[:,0], PCAembedding[:,1], c = label, alpha=0.8)
#plt.show()

#PCA - Singular Value Decomposition
U,s,V = np.linalg.svd(normalizeIrisData.transpose())
print U.shape, s.shape, V.shape
print "s", s
firstTwoCompoment = U[:,0:2]
#PCAembedding = irisData*firstTwoCompoment.transpose();
PCAembedding = irisData*firstTwoCompoment;
print "Emebedding shape: ", PCAembedding.shape
plt.figure()
plt.scatter(PCAembedding[:,0], PCAembedding[:,1], c = label, alpha=0.8)
plt.show()


#PCA - sklearn reference
pca = PCA(n_components=2)
PCAembedding = pca.fit_transform(normalizeIrisData)
plt.figure()
plt.scatter(PCAembedding[:,0], PCAembedding[:,1], c = label, alpha=0.8)
plt.show()

#########################################################################
#LDA - sklearn reference

lda = LDA()
LDAembedding = lda.fit_transform(normalizeIrisData, label)
plt.figure()
plt.scatter(LDAembedding[:,0], LDAembedding[:,1], c = label, alpha=0.8)
plt.show()
